package com.timepies.activity;

import static com.timepies.database.Service.getInstance;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.timepies.R;
import com.timepies.activityutils.DettaglioExpandableList;
import com.timepies.domain.Intervallo;
import com.timepies.domain.MenuPies;
import com.timepies.domain.Timepie;

//http://www.vogella.com/tutorials/AndroidListView/article.html
public class DettaglioPieActivity extends Activity {

	protected Timepie pie = null;
	protected Intervallo interval = new Intervallo();
	protected DettaglioExpandableList listView = null;
	protected Dialog editDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pie_dettaglio);

		try {
			this.pie = (Timepie) getIntent().getSerializableExtra("pieObj");
			this.interval.setStart(Intervallo.date.parse(pie.getData()));
			this.listView = new DettaglioExpandableList(this, interval);
		} catch (Exception e) {
			Log.e(this.toString(), "error : " + e.getMessage());
			Log.e(this.toString(), "error : " + e.getCause());
		}

		refreshTitle();

		Log.d(this.toString(), "onCreate complete!");

	}

	private void refreshTitle() {

		// Titles
		TextView pie_data = (TextView) findViewById(R.id.pie_data);
		TextView pie_testo = (TextView) findViewById(R.id.pie_testo);
		TextView pie_oggetto = (TextView) findViewById(R.id.pie_oggetto);
		pie_data.setText(Intervallo.title.format(interval.getStart()));
		pie_testo.setText(pie.getTesto());
		pie_oggetto.setText(pie.getOggetto());

		// ActionBar
		MenuPies menu = getInstance(this).getMenuPies();
		getActionBar().setTitle(menu.getDescrizione());
		TextView titolo = (TextView) this.findViewById(R.id.label);
		titolo.setBackgroundColor(Color.parseColor(menu.getColore()));

	}

	// declarative call
	public void onClickShareInfo(View view) {
		final EditText edit = new EditText(this);
		final String text = ((TextView) view).getText().toString();

		Builder alert = new Builder(this);
		alert.setTitle("Apporta le ultime modifiche e invia il messaggio a chi vuoi!");
		edit.setText(text);
		alert.setView(edit);
		alert.setPositiveButton("Invia", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int button) {
				Intent intent = new Intent(Intent.ACTION_SEND);
				intent.setType("text/plain");
				intent.putExtra(Intent.EXTRA_TEXT, edit.getText().toString());
				startActivity(Intent.createChooser(intent, "Condividi con:"));
			}
		});
		alert.setNegativeButton("Annulla", null).show();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.dettaglio_menu, menu);

		switch (getInstance(this).getMenuPies()) {

		case FESTIVITA:
		case PONTI:
		case SHAREDPIES:
			MenuItem edit = menu.findItem(R.id.action_edit);
			edit.setVisible(false);
			MenuItem delete = menu.findItem(R.id.action_delete);
			delete.setVisible(false);
			break;
		case TIMEPIES:
			break;

		}

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		final Activity activity = this;

		Log.d(this.toString(), "MenuItem: " + item.getItemId());
		switch (item.getItemId()) {
		case R.id.action_settings:
			startActivity(new Intent(DettaglioPieActivity.this,
					SettingsActivity.class));
			finish();
			break;

		case R.id.action_about:
			startActivity(new Intent(DettaglioPieActivity.this,
					SplashActivity.class));
			finish();
			break;

		case R.id.action_refresh:
			listView.reload();
			Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
			break;

		case R.id.action_edit:
			Intent i = new Intent(this, ModificaPieActivity.class);
			i.putExtra("pieObj", pie);
			i.putExtra("caller", getClass());
			activity.startActivity(i);
			finish();
			break;

		case R.id.action_exit:
			exit();
			break;

		case R.id.action_delete:
			Builder alert = new Builder(DettaglioPieActivity.this);
			alert.setTitle("Cancella Pie");
			alert.setMessage("Sei sicuro di voler cancellare questa data?");
			alert.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int button) {
							Log.d(this.toString(), "PositiveButton " + button);
							Context ctx = getBaseContext();
							getInstance(activity).delete(pie.getId());
							Toast.makeText(ctx, "Eliminato", Toast.LENGTH_SHORT)
									.show();
							exit();
						}
					});
			alert.setNegativeButton("No", null).show();
			break;
		}
		return true;

	}

	@Override
	public void onBackPressed() {
		exit();
	};

	public void exit() {

		listView.threadStop();

		Intent exit = new Intent(DettaglioPieActivity.this,
				ElencoPieActivity.class);
		startActivity(exit);
		finish();
	}

	public Dialog getEditDialog() {
		return editDialog;
	}

	public Timepie getPie() {
		return pie;
	}

	public Intervallo getInterval() {
		return interval;
	}

}